package com.alexsh.web.form.validator;

import com.alexsh.web.form.BookFormBean;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.ResourceBundle;

/**
 * @author Alexander Shmygin <AShmygin@luxoft.com>
 * @version 1.0
 */
public class AddBookValidator implements Validator {

    private static final ResourceBundle LABELS_BUNDLE = ResourceBundle
        .getBundle("labels");

    private static final String DEFAULT_ERROR_MESSAGE = "Field is not specified.";

    public boolean supports(Class clazz) {
        return clazz.equals(BookFormBean.class);
    }

    public void validate(Object obj, Errors errors) {
        BookFormBean formBean = (BookFormBean) obj;
        if (formBean.getTitle() == null || "".equals(formBean.getTitle())) {
            errors.rejectValue("title", "error.empty.field",
                new Object[]{LABELS_BUNDLE.getString("caption.addBookForm.title")},
                DEFAULT_ERROR_MESSAGE);
        }
    }
}
